package org.jeecg.modules.pdd.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pdd.pop.sdk.common.util.JsonUtil;
import com.pdd.pop.sdk.common.util.StringUtils;
import com.pdd.pop.sdk.http.PopClient;
import com.pdd.pop.sdk.http.PopHttpClient;
import com.pdd.pop.sdk.http.api.pop.request.PddDdkGoodsRecommendGetRequest;
import com.pdd.pop.sdk.http.api.pop.response.PddDdkGoodsRecommendGetResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.pdd.config.PddConfig;
import org.jeecg.modules.pdd.entity.PddGoodsCats;
import org.jeecg.modules.pdd.entity.PddGoodsRecommend;
import org.jeecg.modules.pdd.mapper.PddGoodsRecommendMapper;
import org.jeecg.modules.pdd.service.IPddGoodsCatsService;
import org.jeecg.modules.pdd.service.IPddGoodsRecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: 商品推荐管理
 * @Author: jeecg-boot
 * @Date: 2020-05-21
 * @Version: V1.0
 */
@Service
public class PddGoodsRecommendServiceImpl extends ServiceImpl<PddGoodsRecommendMapper, PddGoodsRecommend> implements IPddGoodsRecommendService {


    @Autowired
    private IPddGoodsCatsService pddGoodsCatsService;

    @Override
    public void updateGoodsData() {

        List<PddGoodsCats> pddGoodsCatsList = pddGoodsCatsService.list();
        for (PddGoodsCats pddGoodsCats : pddGoodsCatsList) {
            PopClient client = new PopHttpClient(PddConfig.APP_KEY, PddConfig.APP_SECRET);
            PddDdkGoodsRecommendGetRequest request = new PddDdkGoodsRecommendGetRequest();
            request.setCatId(pddGoodsCats.getCatId());
            PddDdkGoodsRecommendGetResponse response = null;
            try {
                response = client.syncInvoke(request);
            } catch (Exception e) {
                e.printStackTrace();
            }
            String jsonData = JsonUtil.transferToJson(response);
            if (!StringUtils.isEmpty(jsonData)) {
                JSONObject jsonObject = JSON.parseObject(jsonData);
                String goods_search_response = jsonObject.getString("goods_basic_detail_response");
                if (!StringUtils.isEmpty(goods_search_response)) {
                    JSONObject jsonObject1 = JSON.parseObject(goods_search_response);
                    String goodsJsonList = jsonObject1.getString("list");
                    if (!StringUtils.isEmpty(goodsJsonList)) {
                        List<PddGoodsRecommend> pddGoodsRecommendList = JSON.parseArray(goodsJsonList, PddGoodsRecommend.class);
                        if (null != pddGoodsRecommendList && pddGoodsRecommendList.size() > 0) {
                            for (PddGoodsRecommend pddGoodsRecommend : pddGoodsRecommendList) {
                                QueryWrapper queryWrappergoods = new QueryWrapper();
                                queryWrappergoods.in("goods_id", pddGoodsRecommend.getGoodsId());
                                List<PddGoodsRecommend> pddGoodsRecommends = this.baseMapper.selectList(queryWrappergoods);
                                if (null == pddGoodsRecommends||pddGoodsRecommends.size()==0) {
                                    pddGoodsRecommend.setCatIdsStr(pddGoodsRecommend.getCatIds().toString());
                                    save(pddGoodsRecommend);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public Result<?> getPddGoodsRecommend(PddGoodsRecommend pddGoodsRecommend, Integer pageNo, Integer pageSize, HttpServletRequest req) {
        QueryWrapper<PddGoodsRecommend> queryWrapper = QueryGenerator.initQueryWrapper(pddGoodsRecommend, req.getParameterMap());
        Page<PddGoodsRecommend> page = new Page<PddGoodsRecommend>(pageNo, pageSize);
        IPage<PddGoodsRecommend> pageList = this.page(page, queryWrapper);
        return Result.ok(pageList);
    }


}
